package com.oocl.parseMessageImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.oocl.modle.Customer;
import com.oocl.modle.Message;
import com.oocl.parseMessage.IParseMessage;
import com.oocl.util.Valid;

public class ParseMessage implements IParseMessage{
	
	Valid v;
	
	public ParseMessage(Valid v){
		this.v = v;
	}

	public Customer[] parse(Message[] message) {
		List<Customer> list = new ArrayList<Customer>();
		for (int i = 0; i < message.length; i++) {
			String[] info = message[i].getMessage().split("\\s+");
			if(info.length == 5){
				if(!v.validId(info[0])){
					continue;
				}
				if(!v.validName(info[1])){
					continue;
				}
				if(!v.validEmail(info[2])){
					continue;
				}
				if(!v.validSex(info[3])){
					continue;
				}
				if(!v.validBirth(info[4])){
					continue;
				}
				list.add(new Customer(Integer.valueOf(info[0]), info[1],  info[3],info[2], info[4]));
			}
			
		}
		
		Customer[] cs = new Customer[list.size()];
		for (int i = 0; i < cs.length; i++) {
			cs[i] = list.get(i);
		}
		
		return cs;
	}

}
